﻿using System;
using SmartSiteManagementSystem.IRepository.Equipment;
using SmartSiteManagementSystem.Models.Equipment;
using SmartSiteManagementSystem.Repository.DBHelper;
using System.Data;
using Dapper;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using MySql.Data.MySqlClient;

namespace SmartSiteManagementSystem.Repository.Equipment
{
    public class TowerCraneInfoRepository : ITowerCraneInfoRepository
    {
        #region dataTable
        /*
drop table if exists `tower_crane`;
create table `tower_crane`( 
	`tower_crane_id` int(11) not null AUTO_INCREMENT primary key, 
	`tower_crane_name` nvarchar(250) NOT NULL,
    `operating_radius` double(10,2)  NOT NULL default 0,
    `position_x` double(10,2)  NOT NULL default 0,
	`position_y` double(10,2)  NOT NULL default 0,
	`position_z` double(10,2)  NOT NULL default 0,
    `weight` int  NOT NULL default 0,
    `descript` text null,
    `is_delete` bit NOT NULL default 0,
	`create_time` datetime not null default now(),
	`update_time` datetime not null default now()
) default charset=utf8;
         */
        #endregion
        //private IDbConnection _readConn;
        //private IDbConnection _writeConn;

        public bool Delete(Hashtable param)
        {
            bool back = false;
            if (param == null)
                param = new Hashtable();
            String querySql = @"delete from tower_crane";
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where ");
                    int parCount = 0;
                    if (param.ContainsKey("tower_crane_id"))
                    {
                        pars.Add("tower_crane_id", param["tower_crane_id"]);

                        whereCondition.Append(" tower_crane_id =@tower_crane_id");
                        parCount++;
                    }
                }
                string execSql = querySql + whereCondition.ToString();
                int backInt = _writeConn.Execute(execSql, pars);
                if (backInt > 0)
                {
                    back = true;
                }
            }
            return back;
        }

        public TowerCraneInfo Get(Hashtable param)
        {
            TowerCraneInfo back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT 	tower_crane_id as  TowerCraneId, tower_crane_name as TowerCraneName, operating_radius as OperatingRadius,position_x as PositionX,position_y as PositionY,position_z as PositionZ,weight as Weight,descript as Descript,is_delete as IsDelete,create_time as CreateTime,update_time as UpdateTime FROM tower_crane";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {

                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("tower_crane_id"))
                    {
                        pars.Add("tower_crane_id", param["tower_crane_id"]);
                        whereCondition.Append(" and tower_crane_id =@tower_crane_id");
                    }
                    if (param.ContainsKey("TowerCraneName"))
                    {
                        pars.Add("TowerCraneName", param["TowerCraneName"]);
                        whereCondition.Append(" and TowerCraneName =@TowerCraneName");
                    }
                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }

                }
                whereCondition.Append(" limit 1");
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.QueryFirstOrDefault<TowerCraneInfo>(execSql, pars);
            }
            return back;
        }

        public int GetCount(Hashtable param)
        {
            int back = 0;
            if (param == null)
                param = new Hashtable();
            String querySql = "select count(tower_crane_id) from tower_crane";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("tower_crane_id"))
                    {
                        pars.Add("tower_crane_id", param["tower_crane_id"]);
                        whereCondition.Append(" and tower_crane_id =@tower_crane_id");
                    }
                    if (param.ContainsKey("TowerCraneName"))
                    {
                        pars.Add("TowerCraneName", param["TowerCraneName"]);
                        whereCondition.Append(" and TowerCraneName =@TowerCraneName");
                    }
                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }
                }

                string execSql = querySql + whereCondition.ToString();
                #endregion
                back = _readConn.QueryFirstOrDefault<int>(execSql, pars);
            }
            return back;
        }

        public IList<TowerCraneInfo> GetList(Hashtable param, string orderBy, int first, int rows)
        {

            IList<TowerCraneInfo> back = null;
            if (param == null)
                param = new Hashtable();
            String querySql = @"SELECT 	tower_crane_id as  TowerCraneId, tower_crane_name as TowerCraneName, operating_radius as OperatingRadius,position_x as PositionX,position_y as PositionY,position_z as PositionZ,weight as Weight,descript as Descript,is_delete as IsDelete,create_time as CreateTime,update_time as UpdateTime FROM tower_crane";
            using (IDbConnection _readConn = new MySqlConnection(DbHelperOptions.ReadConnectionString))
            {
                #region
                DynamicParameters pars = new DynamicParameters();
                StringBuilder whereCondition = new StringBuilder();
                if (param.Count > 0)
                {
                    whereCondition.Append(" where 1=1");
                    if (param.ContainsKey("tower_crane_id"))
                    {
                        pars.Add("tower_crane_id", param["tower_crane_id"]);
                        whereCondition.Append(" and tower_crane_id =@tower_crane_id");
                    }
                    if (param.ContainsKey("TowerCraneName"))
                    {
                        pars.Add("TowerCraneName", param["TowerCraneName"]);
                        whereCondition.Append(" and TowerCraneName =@TowerCraneName");
                    }
                    if (param.ContainsKey("is_delete"))
                    {
                        pars.Add("is_delete", param["is_delete"]);
                        whereCondition.Append(" and is_delete =@is_delete");
                    }
                }
                if (string.IsNullOrEmpty(orderBy))
                {
                    whereCondition.Append(" order by tower_crane_id desc");
                }
                else
                {
                    whereCondition.Append(" order by @orderBy");
                    pars.Add("orderBy", orderBy);
                }
                whereCondition.Append(" limit @first,@rows");
                pars.Add("first", first);
                pars.Add("rows", rows);
                #endregion
                string execSql = querySql + whereCondition.ToString();
                back = _readConn.Query<TowerCraneInfo>(execSql, pars).AsList<TowerCraneInfo>();
            }
            return back;
        }

        public int Insert(TowerCraneInfo obj)
        {
            int back = 0;
            #region sql
            string sql = @"INSERT INTO tower_crane
(
	tower_crane_name,
    operating_radius,
    position_x,
	position_y,
	position_z,
    weight,
    descript,
    is_delete,
	create_time,
	update_time
)
VALUES
(
	@tower_crane_name,
    @operating_radius,
    @position_x,
	@position_y,
	@position_z,
    @weight,
    @descript,
    @is_delete,
	@create_time,
	@update_time);select last_insert_id();";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                back = _writeConn.QueryFirst<int>(sql, new
                {
                    tower_crane_name=obj.TowerCraneName,
                    operating_radius = obj.OperatingRadius,
                    position_x = obj.PositionX,
                    position_y = obj.PositionY,
                    position_z = obj.PositionZ,
                    weight = obj.Weight,
                    descript = obj.Descript,
                    is_delete = obj.IsDelete,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
            }
            return back;
        }

        public bool Update(TowerCraneInfo obj)
        {
            bool back = false;
            #region sql
            string sql = @"update tower_crane
set
	tower_crane_name=@tower_crane_name,
    operating_radius=@operating_radius,
    position_x=@position_x,
	position_y=@position_y,
	position_z=@position_z,
    weight=@weight,
    descript=@descript,
    is_delete=@is_delete,
	create_time=@create_time,
	update_time=@update_time
WHERE tower_crane_id =@tower_crane_id;";
            #endregion
            using (IDbConnection _writeConn = new MySqlConnection(DbHelperOptions.WriteConnectionString))
            {

                int backint = _writeConn.Execute(sql, new
                {
                    tower_crane_id = obj.TowerCraneId,
                    tower_crane_name = obj.TowerCraneName,
                    operating_radius = obj.OperatingRadius,
                    position_x = obj.PositionX,
                    position_y = obj.PositionY,
                    position_z = obj.PositionZ,
                    weight = obj.Weight,
                    descript = obj.Descript,
                    is_delete = obj.IsDelete,
                    create_time = obj.CreateTime,
                    update_time = obj.UpdateTime
                });
                if (backint > 0)
                {
                    back = true;
                }
            }
            return back;
        }
    }
}

